package com.uuuo.rxjava1;

import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * Created by JiaTao on 2017/4/5.
 */

public class E_Error extends A_Base {
    @Test
    public void onErrorReturn() {
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 6; i++) {
                    if (i == 4) {
                        subscriber.onError(new Exception("number is 4!"));
                    }
                    subscriber.onNext(i);
                }
            }
        }).onErrorReturn(new Func1<Throwable, Integer>() {
            @Override
            public Integer call(Throwable throwable) {
                return 88888;
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                showMsg(integer + "");
            }
        });
    }

    @Test
    public void onErrorResumeNext() {
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 6; i++) {
                    if (i == 4) {
                        subscriber.onError(new Exception("number is 4!"));
                    }
                    subscriber.onNext(i);
                }
            }
        }).onErrorResumeNext(new Func1<Throwable, Observable<? extends Integer>>() {
            @Override
            public Observable<? extends Integer> call(Throwable throwable) {
                return Observable.range(100, 10);
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                showMsg(integer + "");
            }
        });
    }

    @Test
    public void retry() {
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 6; i++) {
                    if (i == 4) {
                        subscriber.onError(new Exception("number is 4!"));
                    }
                    subscriber.onNext(i);
                }
            }
        }).retry(1).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                showMsg(integer + "");
            }
        });
    }

    @Test
    public void retryWhen() {
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 6; i++) {
                    if (i == 4) {
                        subscriber.onError(new IOException("number is 4!"));
                    }
                    subscriber.onNext(i);
                }
            }
        }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
            @Override
            public Observable<?> call(Observable<? extends Throwable> errors) {
                return errors.zipWith(Observable.range(1, 3), new Func2<Throwable, Integer, Integer>() {
                    @Override
                    public Integer call(Throwable throwable, Integer i) {
                        return i;
                    }
                }).flatMap(new Func1<Integer, Observable<? extends Long>>() {
                    @Override
                    public Observable<? extends Long> call(Integer retryCount) {
                        return Observable.timer((long) Math.pow(2, retryCount), TimeUnit.SECONDS);
                    }
                });
            }
        }).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                showMsg(integer + "");
            }
        });
    }

}
